package test;

import Model.BaseModel;
import Model.LoginModel;
import Model.RoomActionModel;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Main {
    private static LoginModel loginModel;
    private static Gson gson = new Gson();

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        Gson gson = new Gson();
        Socket socket = new Socket("127.0.0.1", 8090);
        SocketHelper socketHelper = new SocketHelper(socket);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        String s = socketHelper.readLine();
                        System.out.println(s);
                        BaseModel baseModel = gson.fromJson(s, BaseModel.class);
                        switch (baseModel.getModelType()) {
                            case BaseModel.TYPE_LOGIN_MODEL:
                                loginModel = gson.fromJson(s, LoginModel.class);
                                break;
                            case BaseModel.TYPE_ROOM_ACTION_MODEL:
//                                list.add(NettyUtil.gson.fromJson(s, RoomActionModel.class));
                                break;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        System.out.println("输入用户名：");
        String userName = bf.readLine();
        System.out.println("输入密码：");
        String pwd = bf.readLine();
        loginModel = new LoginModel(userName, pwd);
        socketHelper.writeLineAndFlush(gson.toJson(loginModel));
        System.out.println("1.创建房间\n2.加入房间");
        RoomActionModel roomActionModel = null;
        switch (Integer.parseInt(bf.readLine())) {
            case 1:
                roomActionModel = new RoomActionModel(loginModel.getUserName(), loginModel.getRandomString(), -1, RoomActionModel.NEW_ROOM);
                break;
            case 2:
                roomActionModel = new RoomActionModel(loginModel.getUserName(), loginModel.getRandomString(), Integer.parseInt(bf.readLine()), RoomActionModel.GET_IN_ROOM);
                break;
        }
        socketHelper.writeLineAndFlush(gson.toJson(roomActionModel));
    }
}
